﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace DomainInfoLib
{
    /// <summary>
    /// 时间成本计算器
    /// </summary>
    public class TimeCost
    {

        TimeCostPerItem _TimeCostPerItem = new TimeCostPerItem();

        /// <summary>
        /// 根据查询项估算时间消耗
        /// </summary>
        /// <param name="itemCount"></param>
        /// <returns></returns>
        public string CaculateTimeCost(int itemCount)
        {
            decimal timePerItem = _TimeCostPerItem.GetValue();

            decimal TotalTimeInSecond = itemCount * timePerItem;

            decimal TotalTimeInMintes = TotalTimeInSecond / 60;

            decimal TotalTimeInHours = TotalTimeInMintes / 60;

            decimal TotalTimeInDays = TotalTimeInHours / 24;

            decimal TotalTimeInWeeks = TotalTimeInDays / 7;

            decimal TotalTimeInYears = TotalTimeInDays / 365;

            TimeSpan ts = new TimeSpan(Convert.ToInt64(TotalTimeInSecond * 10000000));

            StringBuilder stb = new StringBuilder();

            stb.AppendLine(ts.Days.ToString() + "天");

            stb.AppendLine(ts.Hours.ToString() + "小时");

            stb.AppendLine(ts.Minutes.ToString() + "分");

            stb.AppendLine(ts.Seconds.ToString() + "秒");

            return stb.ToString();
        }



        public string GetPerTimeCost()
        {
            CurrentCount++;
            
            decimal perCost = CaculatePerCost(CurrentCount,sWatch.ElapsedMilliseconds) ;

            _TimeCostPerItem.SetValue(perCost);
            
            return
                    "数量:" + CurrentCount.ToString() + "\r\n" + "运行时间:" + sWatch.ElapsedMilliseconds.ToString()
                        + "\r\n" + _TimeCostPerItem.GetValue().ToString();
        }

        

        private decimal CaculatePerCost(int CurrentCount, long Elapsedseconds)
        {

            decimal cost =Convert.ToDecimal(Elapsedseconds / CurrentCount)/1000;

            
            return cost;
        }

        public string GetRemainTime()
        {
             return CaculateTimeCost(TotalCount - CurrentCount);
        }

        int TotalCount;
        
        int CurrentCount;

        Stopwatch sWatch=new Stopwatch();

        public void Start(int Count)
        {
            TotalCount = Count;
            
            CurrentCount = 0;

            sWatch.Start();
        }
    }
}
